﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Medianamik.Core.DAL.SQL
{
    public class SelectClause : ISqlQueryPart
    {
        private List<Column> _columns;
        public ReadOnlyCollection<Column> Columns
        {
            get
            {
                return _columns.AsReadOnly(); 
            }
        }

        public SelectClause(Column pColumn)
        {
            _columns = new List<Column>();
            _columns.Add(pColumn);
        }

        public SelectClause AddColumn(Column pColumn)
        {
            _columns.Add(pColumn);
            return this;
        }

        #region ISqlQueryPart Members

        public virtual StringBuilder ToSQL()
        {
            var select = new StringBuilder(" SELECT ");
            select.Append(GetColumnList());

            return select;  
        }


        protected StringBuilder GetColumnList()
        {
            var list = new StringBuilder();
            list.Append(_columns.First().NameWithAlias);
            foreach (var c in Enumerable.Skip(_columns, 1))
            {
                list.Append(", " + c.NameWithAlias);
            }

            return list;
        }
        #endregion
    }
}
